********************************************************************************
*Table: Main model								  		   		  			   * 
*Code to run this file: 													   *
*texdoc do "texdoc do "${workdir}\Do files\Table 2 - main model.do""		   *
********************************************************************************

local trat_nocontrol "Placebo Information `"Social norms"' Trust Framing"

reg vacunada_int i.tratamiento
local n1 = subinstr(string(e(N),"%12.0fc")," ","",.) 
local r21 = string(e(r2), "%9.3f")  
estimates store Model1

reg vacunada_int i.tratamiento dosis_prev_int subsidiado i.ips  i.strata 
local n2 = subinstr(string(e(N),"%12.0fc")," ","",.) 
local r22 = string(e(r2), "%9.3f")  
estimates store Model2

// Generate the table with the estimated effects  
texdoc init "${tables}/T2 main model.tex", replace
texdoc write \begin{table}[h!]
texdoc write \centering
texdoc write \caption{\centering Effect on HPV vaccine uptake} 
texdoc write \small
texdoc write \label{main}
texdoc write \begin{tabular}{lccp{10em}p{10em}} 
texdoc write \hline
texdoc write \addlinespace
texdoc write & & & \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} \\
texdoc write \addlinespace 
texdoc write \hline
texdoc write Treatment & & & & \\  

forval i=1(1)5 {  
 **Modelo 1
   estimates restore Model1  
   local b11  = string( _b[`i'.tratamiento], "%9.3f")
   local se11 = string(_se[`i'.tratamiento], "%9.3f") 
   local df_r11 = e(df_r)
   local pvalue11 = 2*ttail(`df_r11', abs(`b11'/`se11'))  
   stars `pvalue11'
   local stars11 "`stars'" 
   
   local cons1    = string( _b[_cons], "%9.3f")  
   local se_cons1 = string(_se[_cons], "%9.3f")  
   local df_r1 = e(df_r)
   local pv_cons1 = 2*ttail(`df_r1', abs(`cons1'/`se_cons1'))   
   stars `pv_cons1'  
   local stars_cons1 "`stars'"

 **Modelo 2   
   estimates restore Model2  
   local b21  = string( _b[`i'.tratamiento], "%9.3f")  
   local se21 = string(_se[`i'.tratamiento], "%9.3f")  
   local df_r21 = e(df_r)
   local pvalue21 = 2*ttail(`df_r21', abs(`b21'/`se21'))     
   stars `pvalue21'
   local stars21 "`stars'"  
 
   local cons2    = string( _b[_cons], "%9.3f")  
   local se_cons2 = string(_se[_cons], "%9.3f")  
   local df_r2 = e(df_r)
   local pv_cons2 = 2*ttail(`df_r2', abs(`cons2'/`se_cons2'))    
   stars `pv_cons2'  
   local stars_cons2 "`stars'"

   
   if `i' == 0 {  
      local trat_label "Control"  
   }  
   else if `i' == 1 {  
      local trat_label "Placebo"  
   }  
   else if `i' == 2 {  
      local trat_label "Information"  
   }  
   else if `i' == 3 {  
      local trat_label "Social norms"  
   }  
   else if `i' == 4 {  
      local trat_label "Trust"  
   }  
   else if `i' == 5 {  
      local trat_label "Framing"  
   }  
   
   texdoc write \multicolumn{1}{l}{\ \ \ `trat_label'} & & &  \multicolumn{1}{c}{`b11'`stars11'} & \multicolumn{1}{c}{`b21'`stars21'} \\ 
   texdoc write  & & & \multicolumn{1}{c}{(`se11')} & \multicolumn{1}{c}{(`se21')} \\ 
}  

texdoc write & & & &  \\
texdoc write Constant     & & & \multicolumn{1}{c}{`cons1' `stars_cons1'} & \multicolumn{1}{c}{`cons2' `stars_cons2'}  \\
texdoc write 		      & & & \multicolumn{1}{c}{(`se_cons1')}  & \multicolumn{1}{c}{(`se_cons2')}  \\
texdoc write & & & &  \\
texdoc write Observations & & & \multicolumn{1}{c}{`n1'}  & \multicolumn{1}{c}{`n2'}   \\
texdoc write R-squared    & & & \multicolumn{1}{c}{`r21'} & \multicolumn{1}{c}{`r22'} \\
texdoc write & & & &  \\
texdoc write T-test & & & &  \\

forval i = 1/5 {  
   local j = `i' + 1  
   while `j' <= 5 {  

   	  estimates restore Model1 
      test _b[`i'.tratamiento] = _b[`j'.tratamiento]  
      local t1 = string(r(p), "%9.4f")  
	  
	  estimates restore Model2  
      test _b[`i'.tratamiento] = _b[`j'.tratamiento]  
      local t2 = string(r(p), "%9.4f")  
	  
      * Extract the treatment labels  
      local trat_label_i : word `i' of `trat_nocontrol'  
      local trat_label_j : word `j' of `trat_nocontrol'  
       
	  texdoc write \multicolumn{1}{l}{\ \ \ `trat_label_i'} & = & \multicolumn{1}{l}{\ \ \ `trat_label_j'} & \multicolumn{1}{c}{`t1'} & \multicolumn{1}{c}{`t2'} \\
	  
      * Conditionally break the loop if j reaches 5  
      if `j' == 5 {  
        continue, break  
      }  
       
      * Increment the comparison index  
      local j = `j' + 1  
   }  
}

texdoc write \hline
texdoc write Strata fixed effects & & & \multicolumn{1}{c}{No} & \multicolumn{1}{c}{Yes} \\
texdoc write Covariates & & & \multicolumn{1}{c}{No} & \multicolumn{1}{c}{Yes}  \\
	
texdoc write \hline	
texdoc write \addlinespace
texdoc write \multicolumn{5}{p{40em}}{\footnotesize Models 1 and 2 capture the effect of each of the treatments on vaccine take-up following the main model under two especifications. Model 2 includes as covariates whether a girl is under subsidized scheme, whether she has received a dose before, healthcare center visited, and the stratification variable which includes level of income and age. *** p$<$0.001, ** p$<$0.001, * p$<$0.05} \\
texdoc write \end{tabular}
texdoc write \end{table}
texdoc close


		